package com.appspot.peachykene.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.appspot.peachykene.Config;
import com.appspot.peachykene.Email;
import com.appspot.peachykene.impl.GoogleEmailImpl;

public class EmailServlet extends HttpServlet {
	private static final long serialVersionUID = 2080137304524147188L;
	private enum EmailParts { URLBASE, SENDER, SUBJECT, BODY };
	
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp) {
		Email email = new GoogleEmailImpl();
		email.setRecepient(Config.getConfig().getPeachyKeenEmail());
		System.out.println("Sender: " + req.getParameter(EmailParts.SENDER.name()));
		email.setSender(req.getParameter(EmailParts.SENDER.name()));
		email.setSubect(req.getParameter(EmailParts.SUBJECT.name()));
		email.setBody(req.getParameter(EmailParts.BODY.name()));
		String path = req.getParameter(EmailParts.URLBASE.name()) + "?success=" + email.send();
		try {
			resp.sendRedirect(path);
		} catch (IOException ioe) {
			throw new RuntimeException(ioe);
		}	
	}
}
